package com.mt.components.table {
	
	import com.mt.components.utils.ImageSource;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.HBox;
	import mx.controls.Button;
	import mx.controls.ComboBox;
	import mx.controls.Label;
	import mx.events.ListEvent;
	
	/**      
	 * 文件名称：TableBar<br>
	 * 文件描述：分页组件<br>
	 * 创建人：马腾<br>
	 * 创建时间：Nov 25, 2012 6:02:59 PM<br>
	 * 修改人：马腾<br>
	 * 修改时间：Nov 25, 2012 6:02:59 PM<br>
	 */
	public class TableBar extends HBox {
		
		private static var IMAGE_HEIGHT:int = 24;
		private static var IMAGE_WIDTH:int = 24;
		
		public var perPageSizeCombo:ComboBox;
		public var firstBtn:Button;
		public var prevBtn:Button;
		public var nextBtn:Button;
		public var lastBtn:Button;
		public var currentPageCombo:ComboBox;
		public var totalPageLable:Label;
		
		[Bindable]private var totalRecords:int = 0;
		[Bindable]public var submitFunction:Function;
		
		public var currentPage:int = 1;
		[Bindable]public var pageSize:int;
		private var totalPages:int = 0;
		
		[Bindable]private var perPageSizeComboData:ArrayCollection = new ArrayCollection([
			{label:'20行/页',data:20},
			{label:'30行/页',data:30},
			{label:'50行/页',data:50}
		]);
		
		[Bindable]private var currentPageComboData:ArrayCollection = new ArrayCollection();
		
		public function TableBar(){
			super();
			initComponent();
		}
		
		private function initComponent():void{
			perPageSizeCombo = new ComboBox();
			perPageSizeCombo.dataProvider = perPageSizeComboData;
			perPageSizeCombo.width = 90;
			perPageSizeCombo.height = IMAGE_HEIGHT;
			perPageSizeCombo.addEventListener(ListEvent.CHANGE,function(event:ListEvent):void{
				currentPage = 1;
				initPage();
				setButtonEnable();
				loadData();
			});
			
			firstBtn = new Button();
			firstBtn.setStyle("icon" , ImageSource.PAGE_FIRST_ICO);
			firstBtn.width = IMAGE_WIDTH;
			firstBtn.height = IMAGE_HEIGHT;
			firstBtn.addEventListener(MouseEvent.CLICK,function():void{
				currentPage = 1;
				setButtonEnable();
				loadData();
			});
			
			prevBtn = new Button();
			prevBtn.setStyle("icon" , ImageSource.PAGE_PREV_ICO);
			prevBtn.width = IMAGE_WIDTH;
			prevBtn.height = IMAGE_HEIGHT;
			prevBtn.addEventListener(MouseEvent.CLICK,function():void{
				currentPage --;
				setButtonEnable();
				loadData();
			});
			
			nextBtn = new Button();
			nextBtn.setStyle("icon" , ImageSource.PAGE_NEXT_ICO);
			nextBtn.width = IMAGE_WIDTH;
			nextBtn.height = IMAGE_HEIGHT;
			nextBtn.addEventListener(MouseEvent.CLICK,function():void{
				currentPage ++;
				setButtonEnable();
				loadData();
			});
			
			lastBtn = new Button();
			lastBtn.setStyle("icon" , ImageSource.PAGE_LAST_ICO);
			lastBtn.width = IMAGE_WIDTH;
			lastBtn.height = IMAGE_HEIGHT;
			lastBtn.addEventListener(MouseEvent.CLICK,function():void{
				currentPage = totalPages;
				setButtonEnable();
				loadData();
			});
			
			currentPageCombo = new ComboBox();
			currentPageCombo.dataProvider = currentPageComboData;
			currentPageCombo.width = 90;
			currentPageCombo.height = IMAGE_HEIGHT;
			currentPageCombo.addEventListener(ListEvent.CHANGE,function(event:ListEvent):void{
				currentPage = currentPageCombo.selectedItem.data as int;
				setButtonEnable();
				loadData();
			});
			
			totalPageLable = new Label();
			totalPageLable.setStyle("fontWeight" , "bold");
		}
		
		private function setButtonEnable():void{
			if(totalRecords == 0){//总记录数位0，屏蔽所有按钮
				setButton(false);
			}else if(currentPage == 1){//当前也为第一页 屏蔽上一页和第一页按钮
				firstBtn.enabled = false;
				prevBtn.enabled = false;
				if(totalPages == 1){
					nextBtn.enabled = false;
					lastBtn.enabled = false;
				}else{
					nextBtn.enabled = true;
					lastBtn.enabled = true;
				}
				currentPageCombo.enabled = true;
			}else if(currentPage == totalPages){//当前页为最后页，屏蔽下一页和最后页按钮
				firstBtn.enabled = true;
				prevBtn.enabled = true;
				nextBtn.enabled = false;
				lastBtn.enabled = false;
				currentPageCombo.enabled = true;
			}else{//否则 放开所有按钮
				setButton(true);
			}
			//			perPageSizeCombo.enabled = totalRecords > pageSize;
		}
		
		private function setButton(enabled:Boolean):void{
			firstBtn.enabled = enabled;
			prevBtn.enabled = enabled;
			nextBtn.enabled = enabled;
			lastBtn.enabled = enabled;
			currentPageCombo.enabled = enabled;
		}
		
		override protected function commitProperties():void{
			super.commitProperties();
			setStyle("verticalAlign" , "middle");
//			setStyle("borderStyle" , "solid");
			addChild(firstBtn);
			addChild(prevBtn);
			addChild(currentPageCombo);
			addChild(nextBtn);
			addChild(lastBtn);
			addChild(perPageSizeCombo);
			addChild(totalPageLable);
			setTotalRecords(0);
		}
		
		public function loadData():void{
			submitFunction();
		}
		
		public function setTotalRecords(value:int):void{
			this.totalRecords = value;
			initPage();
			setButtonEnable();
			totalPageLable.text = "共" + value + "行";
		}
		
		public function first():int{
			return (currentPage - 1) * pageSize;
		}
		
		private function initPage():void{
			pageSize = perPageSizeCombo.selectedItem.data;
			totalPages = (totalRecords%pageSize == 0)?totalRecords/pageSize:totalRecords/pageSize + 1;
			if(totalRecords == 0){
				currentPage = 1;
			}else{
				if(currentPage < 1){
					currentPage = 1;
				}else if(currentPage > totalPages){
					currentPage = totalPages;
				}
			}
			currentPageComboData.removeAll();
			for(var i:int=1 ; i <= totalPages; i++){
				currentPageComboData.addItem({label:i + '/' + totalPages + "页" , data:i});
			}
			currentPageCombo.selectedIndex = currentPage-1;
		}
	}
}